﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Domain.MainModule.Entities;
using Infraestructure.Data.MainModule.UnitOfWork;
using Infraestructure.CrossCutting.Logging;
using Domain.MainModule.Customers;
using Infraestructure.Data.MainModule.Repositories;

namespace Infraestructure.Data.MainModule.Tests.RepositoriesTests
{
    [TestClass()]
    public class CustomerRepositoryTest: CustomRepositoryTestsBase<Customer, User>
    {
        public override System.Linq.Expressions.Expression<Func<Customer, bool>> FilterExpression
        {
            get { return a => a.UserId == TestConstants.Instance.CUSTOMER_ID; }
        }

        public override System.Linq.Expressions.Expression<Func<Customer, Guid>> OrderByExpression
        {
            get { return a => a.UserId; }
        }

        [TestMethod()]
        public void CustomerPaged_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ICustomerRepository repository = new CustomerRepository(context, traceManager);

            IEnumerable<Customer> customers = repository.GetPagedCustomers(0, 5);

            //Assert
            Assert.IsNotNull(customers);
            Customer c = customers.First();
            Assert.IsNotNull(c.Country.Name);
            Assert.IsNotNull(c.Gender.Description);
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindCustomer_Invoke_NullSpecThrowNewArgumentNullException_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ICustomerRepository repository = new CustomerRepository(context, traceManager);

            //Act
            repository.FindCustomer(null);
        }

        #region CustomerCustomerIdSpecification

        [TestMethod()]
        public void FindCustomerById_Invoke_InvalidUserIdReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ICustomerRepository repository = new CustomerRepository(context, traceManager);
            Customer customer;
            Guid invalidId = new Guid("11111111-1111-1111-1111-111111111111");
            CustomerCustomerIdSpecification spec = new CustomerCustomerIdSpecification(invalidId);

            //Act
            customer = repository.FindCustomer(spec);

            //Assert
            Assert.IsNull(customer);
        }

        /// <summary>
        ///A test for FindCustomer
        ///</summary>
        [TestMethod()]
        public void FindCustomerById_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ICustomerRepository repository = new CustomerRepository(context, traceManager);
            Customer actual;
            CustomerCustomerIdSpecification specification = new CustomerCustomerIdSpecification(TestConstants.Instance.CUSTOMER_ID);

            //Act
            actual = repository.FindCustomer(specification);

            //Assert 
            Assert.IsNotNull(actual);
        }

        #endregion

        #region CustomerMembershipUserIdSpecification

        [TestMethod()]
        public void FindCustomerByMembershipUserId_Invoke_InvalidUserIdReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ICustomerRepository repository = new CustomerRepository(context, traceManager);
            Customer customer;
            Guid invalidId = new Guid("11111111-1111-1111-1111-111111111111");
            CustomerMembershipUserIdSpecification spec = new CustomerMembershipUserIdSpecification(invalidId);

            //Act
            customer = repository.FindCustomer(spec);

            //Assert
            Assert.IsNull(customer);
        }

        /// <summary>
        ///A test for FindCustomer
        ///</summary>
        [TestMethod()]
        public void FindCustomerByMembershipUserId_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ICustomerRepository repository = new CustomerRepository(context, traceManager);
            Customer actual;
            
            CustomerMembershipUserIdSpecification specification = new CustomerMembershipUserIdSpecification(TestConstants.Instance.MEMBERSHIP_USER_ID);

            //Act
            actual = repository.FindCustomer(specification);

            //Assert 
            Assert.IsNotNull(actual);
        }

        #endregion
    }
}
